﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Panny.Data;
using System.ComponentModel;
namespace Model
{
    [DataObject()]
   public class CorpInfoData
   {
       #region select method

        [DataObjectMethod(DataObjectMethodType.Select)]
       public DataSet SelectSingle(int CorpID)
       {
           string sql = "select * from CorpInfo where CorpID=@CorpID";
           using (SqlText selection = new SqlText(sql))
           {
               selection.AddParameter("@CorpID", CorpID);
               return selection.GetDataset();
           }
       }

       public DataSet SelectSingle(string markName)
       {
           string sql = "select * from CorpInfo where MarkName=@MarkName";
           using (SqlText selection = new SqlText(sql))
           {
               selection.AddParameter("@MarkName", markName);
               return selection.GetDataset();
           }
       }
       [DataObjectMethod(DataObjectMethodType.Select)]
       public DataSet So(string key,string type,string city)
       {
           StringBuilder sql = new StringBuilder();
           sql.Append("select a.Hits,a.Address,a.Title,b.TypeID,b.TypeName,a.CorpID,a.ImageSrc,a.CreateOn from CorpInfo a ");
           sql.Append(" inner join CorpType b  on a.TypeID = b.TypeID where 1=1 ");
           if (!string.IsNullOrEmpty(key))
           {
               sql.AppendFormat(" and (a.keys like '%{0}%' or a.Title like '%{0}%' ) ", key);
           }
           if (!string.IsNullOrEmpty(type))
           {
               sql.AppendFormat(" and ('{0}'='0' or a.TypeID ={0})", type);
           }
           if (!string.IsNullOrEmpty(city))
           {
               sql.AppendFormat("  and a.AreaID = {0}", city);
           }
           sql.Append(" order by a.hits desc");
           using (SqlText selection = new SqlText(sql.ToString()))
           {
               return selection.GetDataset();
           }
       }

       /// <summary>
       /// 获取列表
       /// </summary>
       /// <param name="userid"></param>
       /// <returns></returns>
       [DataObjectMethod(DataObjectMethodType.Select)] 
       public DataSet GetListByUser(string username)
       {
           StringBuilder sql = new StringBuilder();
           sql.Append("select CorpID,Title,ImageSrc,Telphone,Address,Keys,(select count(videoid) from CorpVideo where CorpVideo.CorpID=CorpInfo.CorpID ) as TvAcount ");
           sql.Append(" from  CorpInfo where UserName = @UserName");
           using (SqlText selection = new SqlText(sql.ToString()))
           {
               selection.AddParameter("@UserName", username);
               return selection.GetDataset();
           }
          
       }


       #endregion

       #region 搜索

       /// <summary>
       /// 红店铺
       /// </summary>
       /// <param name="topNum">数量</param>
       /// <param name="typeid">类型</param>
       /// <returns></returns>
       public DataSet SoTopHot(int topNum, int? typeid, int? cityid)
       {

           if (topNum <= 0)
               topNum = 1;
           StringBuilder sql = new StringBuilder();
           sql.AppendFormat("select top {0} ImageSrc,Hits,CorpID,Title,Address,CorpInfo.TypeID,b.TypeName,Telphone from CorpInfo inner join CorpType b on CorpInfo.TypeID = b.TypeID where ({1}<=0 or {1} is null or AreaID = '{1}') ", topNum, cityid);
           if (typeid != null && typeid > 0)
           {
               sql.AppendFormat(" and CorpInfo.TypeID={0} ", typeid);
           }

           sql.Append(" order by Hits desc,CorpInfo.Sort");
           using (SqlText selection = new SqlText(sql.ToString()))
           {
               return selection.GetDataset();
           }
       }

       /// <summary>
       /// 推荐店铺
       /// </summary>
       /// <param name="topNum">数量</param>
       /// <param name="typeid">类型</param>
       /// <returns></returns>
       public DataSet SoTopGood(int topNum, int? typeid, int? cityid)
       {
           StringBuilder sql = new StringBuilder();
           sql.AppendFormat("select top {0} ImageSrc,Hits,CorpID,Title,Address,TypeID,b.TypeName form CorpInfo inner join CorpType b on CorpInfo.TypeID = b.TypeID where ({1}<=0 or {1} is null or AreaID = '{1}') ", topNum, cityid);
           if (typeid != null && typeid > 0)
           {
               sql.AppendFormat(" and TypeID={0} ", typeid);
           }
           sql.Append(" order by Sort,Hits desc");
           using (SqlText selection = new SqlText(sql.ToString()))
           {
               return selection.GetDataset();

           }
       }

       /// <summary>
       /// 最新加盟
       /// </summary>
       /// <param name="topNum">数量</param>
       /// <param name="typeid">类型</param>
       /// <returns></returns>
       public DataSet SoTopNew(int topNum, int? typeid, int? cityid)
       {

           StringBuilder sql = new StringBuilder();
           sql.AppendFormat("select top {0} ImageSrc,Hits,CorpID,Title,Address,TypeID,b.TypeName form CorpInfo inner join CorpType b on CorpInfo.TypeID = b.TypeID where ({1}<=0 or {1} is null or AreaID = '{1}') ", topNum, cityid);
           if (typeid != null && typeid > 0)
           {
               sql.AppendFormat(" and TypeID={0} ", typeid);
           }
           sql.Append(" order by CreateOn desc,Sort,Hits desc");
           using (SqlText selection = new SqlText(sql.ToString()))
           {
               return selection.GetDataset();
           }
       }


       #endregion 


       #region update

       public int UpdateImage(string path, string corpID)
       {
           StringBuilder sql = new StringBuilder();
           sql.Append(" update CorpInfo set ImageSrc = @Path where CorpID=@CorpID");
           using (SqlText updation = new SqlText(sql.ToString()))
           {
               updation.AddParameter("@Path", path);
               updation.AddParameter("@CorpID", corpID);
               return updation.ExecuteNonQuery();
           }
       }

       #endregion


       #region insert
       [DataObjectMethod(DataObjectMethodType.Insert)]
       public int Insert(Entity.CorpInfo obj)
       {
           using (SqlStoredProcedure insertion = new SqlStoredProcedure("Pro_Pan_CorpInfo_Insert"))
           {
               insertion.AddParameter("@MarkName", obj.MarkName);
               insertion.AddParameter("@Title", obj.Title);
               insertion.AddParameter("@AreaID", obj.AreaID);
               insertion.AddParameter("@Address", obj.Address);
               insertion.AddParameter("@Telphone", obj.Telphone);
               insertion.AddParameter("@Fax", obj.Fax);
               insertion.AddParameter("@TypeID", obj.TypeID);
               insertion.AddParameter("@WorkTime", obj.WorkTime);
               insertion.AddParameter("@Summary", obj.Summary);
               insertion.AddParameter("@Site", obj.Site);
               insertion.AddParameter("@Consume", obj.Consume);
               insertion.AddParameter("@MapLocation", obj.MapLocation);
               insertion.AddParameter("@Keys", obj.Keys);
               insertion.AddParameter("@BusLine", obj.BusLine);
               insertion.AddParameter("@Remark", obj.Remark);
               insertion.AddParameter("@UserName", obj.UserName);

               try
               {
                   insertion.ExecuteNonQuery();
               }
               catch (Exception e)
               {
                   throw new Exception("插入的时候出错", e);
               }
               return insertion.SelectIdentity();
           }
       }
       #endregion
   }
}
